//
//  HotelFacilitiesViewModel.swift
//  AirTravel
//
//  Created by apple on 2021/7/20.
//

import UIKit

protocol HotelFacilitiesDelegate:NSObjectProtocol {
    func hotelFacilitiesClick(data:NSDictionary)
}

class HotelFacilitiesViewModel: NSObject {

    weak var delegate: HotelFacilitiesDelegate?
    var collView : UICollectionView?
    let identifyF = "FacilitiesColl"
    let identifyTC = "TagColl"
    
    var dataSouce:[String] = []
    
    var number = 0
    
    var  currentTag = 1
    
    var closure:ClosureInt_Void?
    
    
    func setUpdata(arrat:Array<String>) {
        self.dataSouce = arrat
        self.collView?.reloadData()
    }
    func bindViewToViewModel(collView : UICollectionView,number:Int) {
        self.collView = collView
        self.collView?.dataSource = self
        self.collView?.delegate = self
        self.number = number
        if number == 0 {
            let layout = UICollectionViewFlowLayout.init()
            layout.scrollDirection = .vertical
            layout.itemSize = .init(width: 100, height: 30)
            layout.minimumInteritemSpacing = 8
            layout.minimumLineSpacing = 10
            layout.sectionInset = UIEdgeInsets.init(top: 10, left: 15, bottom: 10, right: 15)
            self.collView?.collectionViewLayout = layout
        }else if number == 2{
            let layout = UICollectionViewFlowLayout.init()
            layout.scrollDirection = .vertical
            layout.itemSize = .init(width: (SCREEN_WIDTH - 100) / 5, height: 26)
            layout.minimumInteritemSpacing = 10
            layout.minimumLineSpacing = 10
            layout.sectionInset = UIEdgeInsets.init(top: 10, left: 15, bottom: 10, right: 15)
            self.collView?.collectionViewLayout = layout
        }
        else{
            let layout = UICollectionViewFlowLayout.init()
            layout.scrollDirection = .vertical
            layout.itemSize = .init(width: (SCREEN_WIDTH - 36 - 24) / 3, height: 30)
            layout.minimumInteritemSpacing = 10
            layout.minimumLineSpacing = 12
            layout.sectionInset = UIEdgeInsets.init(top: 10, left: 18, bottom: 10, right: 18)
            self.collView?.collectionViewLayout = layout
        }
        
        self.collView?.isScrollEnabled = false
        
        self.collView?.showsVerticalScrollIndicator = true
        
        self.collView?.register(UINib.init(nibName: "FacilitiesColl", bundle: nil), forCellWithReuseIdentifier: identifyF)
        self.collView?.register(UINib.init(nibName: "TagColl", bundle: nil), forCellWithReuseIdentifier: identifyTC)
    }
}

extension HotelFacilitiesViewModel:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if self.number == 0 {
            return dataSouce.count
        }else if self.number == 2{
            return 10
        }
        else{
            return 6
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch self.number {
        case 0:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifyF, for: indexPath) as! FacilitiesColl
            cell.titleLab.text = dataSouce[indexPath.row]
            return cell
        case 2:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifyTC, for: indexPath) as! TagColl
            cell.contentLab.layer.cornerRadius = 2
            cell.contentLab.layer.borderWidth = 1
            cell.contentLab.layer.borderColor = UIColor(red: 235.0 / 255.0, green: 235.0 / 255.0, blue: 235.0 / 255.0, alpha: 1.0).cgColor
            cell.contentLab.backgroundColor = .white
            cell.contentLab.text = "\(indexPath.row)"
            
            if(currentTag == indexPath.row){
                cell.contentLab.textColor = .white
                
                cell.contentLab.backgroundColor = UIColor.init(hexString: "FF6635")
            }
            return cell
        default:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifyTC, for: indexPath) as! TagColl
            cell.contentLab.backgroundColor = UIColor(red: 252.0 / 255.0, green: 238.0 / 255.0, blue: 232.0 / 255.0, alpha: 1.0)
            cell.contentLab.text = "服务热情(221)"
            return cell
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if(self.number == 2){
            
            for i in 0..<10 {
                let cell = collectionView.cellForItem(at: IndexPath(row: i, section: 0)) as! TagColl
                cell.contentLab.backgroundColor = .white
                cell.contentLab.textColor = UIColor.init(hexString: "666666")
            }
            
            
            currentTag = indexPath.row
            
            let cell = collectionView.cellForItem(at: indexPath) as! TagColl
            
            cell.contentLab.textColor = .white
            
            cell.contentLab.backgroundColor = UIColor.init(hexString: "FF6635")
            
            closure!(currentTag)
        }
    }
    

}
